package JavaDowork.pratice6;

import java.util.Scanner;

public class Q8 {

    public static void main(String[] args) {
        // 从控制台输入矩阵的行数和列数
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入矩阵的行数：");
        int rows = scanner.nextInt();
        System.out.print("请输入矩阵的列数：");
        int cols = scanner.nextInt();

        // 定义并初始化矩阵
        int[][] matrix = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print("请输入第" + (i + 1) + "行第" + (j + 1) + "个元素的值：");
                matrix[i][j] = scanner.nextInt();
            }
        }

        // 计算主对角线元素之和
        int mainDiagonalSum = 0;
        for (int i = 0; i < rows; i++) {
            mainDiagonalSum += matrix[i][i];
        }

        // 计算副对角线元素之和
        int subDiagonalSum = 0;
        for (int i = 0; i < rows; i++) {
            subDiagonalSum += matrix[i][cols - i - 1];
        }

        // 输出主对角线和副对角线元素之和
        System.out.println("主对角线元素之和：" + mainDiagonalSum);
        System.out.println("副对角线元素之和：" + subDiagonalSum);
    }
}
